from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.key_do import SysUserKey
from module_admin.entity.vo.key_vo import KeyModel, KeyPageQueryModel
from utils.page_util import PageUtil


class KeyDao:
    """
    UserKey模块数据库操作层
    """

    @classmethod
    async def get_key_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据ID获取UserKey详细信息

        :param db: orm对象
        :param id: ID
        :return: UserKey信息对象
        """
        key_info = (
            (await db.execute(select(SysUserKey).where(SysUserKey.id == id)))
            .scalars()
            .first()
        )

        return key_info

    @classmethod
    async def get_key_detail_by_key(cls, db: AsyncSession, api_key: str):
        """
        根据ID获取UserKey详细信息

        :param db: orm对象
        :param id: ID
        :return: UserKey信息对象
        """
        key_info = (
            (await db.execute(select(SysUserKey).where(SysUserKey.api_key == api_key)))
            .scalars()
            .first()
        )

        return key_info

    @classmethod
    async def get_key_detail_by_info(cls, db: AsyncSession, key: KeyModel):
        """
        根据UserKey参数获取UserKey信息

        :param db: orm对象
        :param key: UserKey参数对象
        :return: UserKey信息对象
        """
        key_info = (
            (
                await db.execute(
                    select(SysUserKey).where(
                        SysUserKey.api_key == key.api_key if key.api_key else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return key_info

    @classmethod
    async def get_key_list(
        cls, db: AsyncSession, query_object: KeyPageQueryModel, is_page: bool = False
    ):
        """
        根据查询参数获取UserKey列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: UserKey列表信息对象
        """
        query = (
            select(SysUserKey)
            .where(
                (
                    SysUserKey.api_key == query_object.api_key
                    if query_object.api_key
                    else True
                ),
                (
                    SysUserKey.user_id == query_object.user_id
                    if query_object.user_id
                    else True
                ),
            )
            .order_by(SysUserKey.id)
            .distinct()
        )
        key_list = await PageUtil.paginate(
            db, query, query_object.page_num, query_object.page_size, is_page
        )

        return key_list

    @classmethod
    async def add_key_dao(cls, db: AsyncSession, key: KeyModel):
        """
        新增UserKey数据库操作

        :param db: orm对象
        :param key: UserKey对象
        :return:
        """
        db_key = SysUserKey(
            **key.model_dump(
                exclude={
                    "id",
                }
            )
        )
        db.add(db_key)
        await db.flush()

        return db_key

    @classmethod
    async def edit_key_dao(cls, db: AsyncSession, key: dict):
        """
        编辑UserKey数据库操作

        :param db: orm对象
        :param key: 需要更新的UserKey字典
        :return:
        """
        await db.execute(update(SysUserKey), [key])

    @classmethod
    async def delete_key_dao(cls, db: AsyncSession, key: KeyModel):
        """
        删除UserKey数据库操作

        :param db: orm对象
        :param key: UserKey对象
        :return:
        """
        await db.execute(delete(SysUserKey).where(SysUserKey.id.in_([key.id])))
